package drukarnia.controller;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;

import javax.servlet.jsp.JspWriter;
 
public class JFTP {
 
    /**
     * JavaProgrammingForums.com
     */
	public static String server = "83.14.244.3";
    public static String userName = "finezja";
    public static String password = "123qwe";
//    public static String server = "ftp.misiekmk.linuxpl.info";
//    public static String userName = "misiekmk";
//    public static String password = "stfzv6095";

 
    public void download(String fileName,JspWriter ouut, String katalog) throws Exception{
 
    ouut.println("Connecting to FTP server...<br>");    
    System.out.println("katalogggg: "+katalog);
    //Connection String
    URL url = new URL("ftp://"+userName+":"+password+"@"+server+"/druk/"+katalog+"/"+fileName+";type=i");
    
//    URL url = new URL("ftp://"+userName+":"+password+"@"+server+"/druk/misiekAlbum/"+fileName+";type=i");
    //URL url = new URL("ftp://"+userName+":"+password+"@"+server+"/"+fileName+";type=i");
    URLConnection con = url.openConnection();
 
    BufferedInputStream in = new BufferedInputStream(con.getInputStream());
 
    ouut.println("Downloading file... <br>");
 
    //Downloads the selected file to the C drive
    FileOutputStream out = new FileOutputStream("D:\\" + fileName);
 
    int i = 0;
    byte[] bytesIn = new byte[1024];
    while ((i = in.read(bytesIn)) >= 0) {
        out.write(bytesIn, 0, i);
    }
    out.close();
    in.close();
 
    ouut.println("File downloaded.");
 
    }
 
}